package com.system.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.member.domain.R;
import com.system.member.domain.SysMenu;
import com.system.member.domain.SysRole;
import com.system.member.domain.SysUser;
import com.system.member.enums.CaptchaEnums;
import com.system.member.mapper.SysMenuMapper;
import com.system.member.mapper.SysRoleMapper;
import com.system.member.mapper.SysUserMapper;
import com.system.member.req.UserRegisterReq;
import com.system.member.service.NotifyService;
import com.system.member.service.SysUserService;
import com.system.member.utils.UIDGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author java1234
* @description 针对表【sys_user】的数据库操作Service实现
* @createDate 2022-08-30 10:49:41
*/
@Service
@Primary
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService {
    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    private NotifyService notifyService;
    @Override
    public SysUser getByEmail(String email) {
        return getOne(new QueryWrapper<SysUser>().eq("email",email));
    }

    @Override
    public String getUserAuthorityInfo(Long userId) {
       return null;
    }

    @Override
    public R register(UserRegisterReq req) {
        boolean checkCode = false;
        //校验验证码
        if (StringUtils.isNotBlank(req.getTo())) {
            checkCode = notifyService.checkCode(req.getTo(),req.getCode(), CaptchaEnums.USER_REGISTER.getCode());
        }
        if (!checkCode) {
            return R.error("验证码校验失败！！");
        }
        if(!checkUnique(req.getTo())){
            return R.error("该手机已经注册！！");
        }
        SysUser sysUser = new SysUser();
        sysUser.setId(UIDGenerator.next());
        sysUser.setPhonenumber(req.getTo());
        sysUser.setUsername("测试账户");
        sysUserMapper.insert(sysUser);
        return R.ok("注册成功！!");
    }

    @Override
    public R login(UserRegisterReq req) {
        boolean checkCode = false;
        //校验验证码
        if (StringUtils.isNotBlank(req.getTo())) {
            checkCode = notifyService.checkCode(req.getTo(),req.getCode(), CaptchaEnums.USER_LOGIN.getCode());
        }
        if (!checkCode) {
            return R.error("手机验证码校验失败！！");
        }
        return R.ok("登录成功！！");
    }


    protected Boolean checkUnique(String phonenumber){
        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.eq("phonenumber",phonenumber);
        List<SysUser> sysUsers = sysUserMapper.selectList(sysUserQueryWrapper);
        return sysUsers.size()>0 ? false : true;
    }
}




